package _2观察者模式.多主题;

import java.util.ArrayList;

// 气象站
class WeatherStation implements Subject {
    String forecastTime, forecastMess;
    int maxTemperature, minTemperature;
    ArrayList<Observer> observerList;

    WeatherStation() {
        this.observerList = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        if (null == observer) {
            return;
        }
        if (!observerList.contains(observer)) {
            this.observerList.add(observer);
        }
    }

    @Override
    public void deleteObserver(Observer observer) {
        if (observerList.contains(observer)) {
            this.observerList.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(this);
        }
    }

    public void doForecast(String t, String mess, int max, int min) {
        this.forecastTime = t;
        this.forecastMess = mess;
        this.maxTemperature = max;
        this.minTemperature = min;
        this.notifyObserver();
    }

    public String getForecastTime() {
        return forecastTime;
    }

    public String getForecastMess() {
        return forecastMess;
    }

    public int getMaxTemperature() {
        return maxTemperature;
    }

    public int getMinTemperature() {
        return minTemperature;
    }
}

// 旅行社
class TravelAgency implements Subject {
    String tourStartTime;
    String tourMess;
    ArrayList<Observer> observerList;

    TravelAgency() {
        this.observerList = new ArrayList<>();
    }

    @Override
    public void addObserver(Observer observer) {
        if (null == observer) {
            return;
        }
        if (!observerList.contains(observer)) {
            this.observerList.add(observer);
        }
    }

    @Override
    public void deleteObserver(Observer observer) {
        if (observerList.contains(observer)) {
            this.observerList.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(this);
        }
    }

    public void giveMess(String time, String mess){
        this.tourStartTime = time;
        this.tourMess = mess;
        notifyObserver();
    }

    public String getTourStartTime() {
        return tourStartTime;
    }

    public String getTourMess() {
        return tourMess;
    }
}

class Person implements Observer {
    Subject subjectOne, subjectTwo;
    String forecastTime, forecastMess;
    int maxTemperature, minTemperature;
    String tourStartTime, tourMess;

    Person(Subject subjectOne, Subject subjectTwo) {
        this.subjectOne = subjectOne;
        this.subjectTwo = subjectTwo;
        subjectOne.addObserver(this);
        subjectTwo.addObserver(this);
    }

    @Override
    public void update(Subject subject) {
        if (subject instanceof WeatherStation) {
            WeatherStation weatherStation = (WeatherStation) subject;
            forecastTime = weatherStation.getForecastTime();
            forecastMess = weatherStation.getForecastMess();
            maxTemperature = weatherStation.getMaxTemperature();
            minTemperature = weatherStation.getMinTemperature();
            System.out.print("预报日期：" + forecastTime + ",");
            System.out.print("天气情况：" + forecastMess + ",");
            System.out.print("最高温度：" + forecastMess + ",");
            System.out.println("最低温度：" + forecastMess + ",");
        } else if (subject instanceof TravelAgency) {
            TravelAgency travelAgency = (TravelAgency) subject;
            tourStartTime = travelAgency.getTourStartTime();
            tourMess = travelAgency.getTourMess();
            System.out.print("旅游开始日期：" + tourStartTime + ",");
            System.out.println("旅游信息：" + tourMess + "。");
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        WeatherStation weatherStation = new WeatherStation();
        TravelAgency travelAgency = new TravelAgency();
        Person person = new Person(weatherStation, travelAgency);
        weatherStation.doForecast("7月","阴有小雨",28,20);
        travelAgency.giveMess("7月","凤凰3日游");
        weatherStation.doForecast("11月","阴转多云",30,22);
        travelAgency.giveMess("11月","长沙7日游");
    }
}
